package enterprise

import (
	"context"
	"errors"
	"fmt"
	"github.com/google/uuid"
	"gorm.io/gorm"
	"insighttracking/apps/admin/internal/logic/area"
	"insighttracking/apps/admin/internal/svc"
	"insighttracking/apps/admin/internal/types"
	"insighttracking/query/dao"
	"net/http"

	"github.com/zeromicro/go-zero/core/logx"
	"os"
)

type ModifyEnterpriseByIdApiLogic struct {
	logx.Logger
	ctx     context.Context
	svcCtx  *svc.ServiceContext
	Request *http.Request
}

func NewModifyEnterpriseByIdApiLogic(ctx context.Context, svcCtx *svc.ServiceContext, request *http.Request) *ModifyEnterpriseByIdApiLogic {
	hostname, _ := os.Hostname()
	return &ModifyEnterpriseByIdApiLogic{
		Logger: logx.WithContext(ctx).WithCallerSkip(0).WithFields(
			logx.Field("Log_UUID", uuid.New().String()),
			logx.Field("hostname", hostname)),
		ctx:     ctx,
		svcCtx:  svcCtx,
		Request: request,
	}
}

func (l *ModifyEnterpriseByIdApiLogic) ModifyEnterpriseByIdApi(req *types.ModifyEnterpriseReq) (resp string, err error) {
	// todo: add your logic here and delete this line
	_, err = dao.EnterpriseEntity.WithContext(l.ctx).Where(dao.EnterpriseEntity.ID.Eq(req.Id)).First()
	if errors.Is(err, gorm.ErrRecordNotFound) {
		return "", err
	}
	enterpriseEntity, err := dao.EnterpriseEntity.WithContext(l.ctx).Where(
		dao.EnterpriseEntity.ID.Eq(req.Id),
		dao.EnterpriseEntity.Name.Eq(req.Name),
	).First()
	if !errors.Is(err, gorm.ErrRecordNotFound) && enterpriseEntity.ID != req.Id {
		return "", errors.New("名字已经存在,不能重复")
	}
	areaLogic := area.NewGetAreaTreeApiLogic(l.ctx, l.svcCtx, l.Request)
	var fillAddress = ""
	if req.TownCode != "" {
		fillAddress, _ = areaLogic.GetAreaNameApi(req.TownCode)
	} else {
		fillAddress, _ = areaLogic.GetAreaNameApi(req.CountyCode)
	}
	if _, err = dao.EnterpriseEntity.WithContext(l.ctx).Where(dao.EnterpriseEntity.ID.Eq(req.Id)).UpdateSimple(
		dao.EnterpriseEntity.Name.Value(req.Name),                                               // 企业名称
		dao.EnterpriseEntity.Person.Value(req.Person),                                           // 负责人
		dao.EnterpriseEntity.Mobile.Value(req.Mobile),                                           // 联系电话
		dao.EnterpriseEntity.Logo.Value(req.Logo),                                               // 企业logo
		dao.EnterpriseEntity.Status.Value(req.Status),                                           // 状态0：营业  1：停业
		dao.EnterpriseEntity.ProvinceCode.Value(req.ProvinceCode),                               // 省份id
		dao.EnterpriseEntity.CityCode.Value(req.CityCode),                                       // 城市id
		dao.EnterpriseEntity.CountyCode.Value(req.CountyCode),                                   // 县、区id
		dao.EnterpriseEntity.TownCode.Value(req.TownCode),                                       // 乡镇code
		dao.EnterpriseEntity.Address.Value(req.Address),                                         // 具体地址
		dao.EnterpriseEntity.FullAddress.Value(fmt.Sprintf("%s/%s,", fillAddress, req.Address)), // 具体地址
	); err != nil {
		return "", errors.New("修改失败")
	}
	return "修改成功", nil
}
